package com.xiaochong.tir.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaochong.service.common.config.oss.OSSTool;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.userclient.UserServiceClient;
import com.xiaochong.tir.common.data.vo.BtUser;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import com.xiaochong.tir.common.data.mapper.BtUserMapper;

/**
 * 此类为[基础代码]自动生成,已经拥有基本的增删改成操作 
 * <其他请自行扩展>
 * @author cx
 * @time 2018-07-25 10:48
 * 
 */
@Service
public class BtUserService{
 
      
    @Autowired
	private BtUserMapper btUserDao;

    @Autowired
    UserServiceClient userServiceClient;

    @Autowired
    private RedisServer redisServer;

    @Autowired
    private OSSTool ossTool;

	/**
     * 新增
     * @param entity 
     * @param session
     */
    public int insertSelective (BtUser entity) {
     return this.btUserDao.insertSelective(entity);
    }	
    
    /**
     * 批量新增
     * @param list 
     * @param session
     */
    public void insertBatch(List<BtUser> list) {
           
          this.btUserDao.insertBatch(list);
         
         
    }
    
    /**
     * 根据主键修改单条数据
     * @param entity 
     * @param session
     */
    public int updateByPrimaryKey(BtUser entity) {
           return this.btUserDao.updateByPrimaryKey(entity);
    }
    
    /**
     * 根据主键批量修改数据
     * @param list 
     * @param session
     */
    public void updateBatch(List<BtUser> list) {
           
          this.btUserDao.updateBatch(list);
          
        
    }
    
    /**
     * 根据主键查询单条数据
     * @param id
     */
    public BtUser selectByPrimaryKey(Integer id) {
            return this.btUserDao.selectByPrimaryKey(id);
    }
    
    /**
     * 根据条件查询单条记录
     * @param entity 
     */
    public BtUser selectByEntity(BtUser entity) {
           return this.btUserDao.selectByEntity(entity);
    }
    
    
    /**
     * 根据条件查询多条记录
     * @param entity 
     */
    public List<BtUser> selectByEntityList(BtUser entity) {
           return  this.btUserDao.selectByEntityList(entity);
    }
    
    /**
     * 根据条件查询Id
     * @param entity 
     */
    public Integer selectById(BtUser entity) {
           return  this.btUserDao.selectById(entity);
    }
    
     /**
     * 根据条件查询Ids
     * @param entity 
     */
    public List<Integer> selectByIds(BtUser entity) {
        return  this.btUserDao.selectByIds(entity);
    }
    
     /**
     * 根据主键删除单条记录
     * @param id
     */
    public void deleteByPrimaryKey(Integer id) {
           this.btUserDao.deleteByPrimaryKey(id);
    }
    
    /**
     * 根据主键删除多条记录
     * @param list
     */
    public void deleteBatch(List<Integer> list) {
           this.btUserDao.deleteBatch(list);
    }
    
     /**
     * 根据某些条件删除
     * @param entity
     */
    public void deleteBatch(BtUser entity) {
           this.btUserDao.deleteByEntity(entity);
    }

    /**
     * 刷新用户缓存
     * @param entity
     */
    public void setUserCache(String userToken){

        String flagStr = userServiceClient.checkUserToken(userToken);
        if(StringUtils.isNotBlank(flagStr)){
            JSONObject jsonObject = JSON.parseObject(flagStr);
            JSONObject json = new JSONObject(  );
            String code = jsonObject.getString( "code" );
            if("000000".equals(code)){//验证登录成功
                //刷新缓存
                if(jsonObject.containsKey( "data" )){
                    JSONObject jsonObject1=jsonObject.getJSONObject( "data" ).getJSONObject( "result" );
                    if(jsonObject1.containsKey( "mobile" )||jsonObject1.containsKey( "email" )){
                        String mobile=jsonObject1.getString( "mobile" );
                        String email=jsonObject1.getString( "email" );
                        BtUser btUser = new BtUser();
                        btUser.setEmail( email );
                        btUser.setMobile( mobile );
                        List<BtUser> btList = btUserDao.selectByAccount( btUser );
                        if(btList.size()>0){
                            BtUser btUser1 = btList.get( 0 );
                            JSONObject data=new JSONObject(  );
                            json.put( "msg", jsonObject.get( "msg" ));
                            json.put( "traceId", jsonObject.get( "traceId" ));
                            json.put( "code", jsonObject.get( "code" ));
                            data.put( "token",userToken );
                            data.put( "userId",btUser1.getId() );
                            data.put( "userName",btUser1.getImageUrl() );
                            data.put( "updateTime",jsonObject1.get( "updateTime" ) );
                            data.put( "platform",jsonObject1.get( "platform" ) );
                            data.put( "countryNum",jsonObject1.get( "countryNum" ) );
                            data.put( "status",jsonObject1.get( "status" ) );
                            data.put( "createTime",jsonObject1.get( "createTime" ) );
                            if(btUser1.getImageUrl()!=null){
                                String imgUrl=ossTool.getOSSUrl( btUser1.getImageUrl() );
                                data.put( "imageUrl",imgUrl );
                            }
                            json.put( "data",data );
                        }
                    }
                }
                redisServer.set( RedisKeysUtils.TIR_USER_TOKEN+ userToken,JSON.toJSONString( json ),86400L );
            }
        }

    }
   
}